#usr/bin/python
import networkx as nx
from PyQt4.QtGui import * 
from PyQt4.QtGui import * 
import pySpec as py


#from core import graph_algorithm as ga

class Drawing_graph(py.PySpecBasis):#(QWidget)
	def __init__(self,  peaks, drawing_algorithm,  parent=None):
		#QWidget.__init__(self,  parent)
		self.peaks = peaks
		#self.graph =self.make_spl_graph()
		#self.nodes =self.graph.nodes()
		#self.edges =self.graph.edges()
		#self.points = ga.random_layout(len(self.nodes), self.size())
	
	def make_cluster_graph(self):
		pass
	def make_spl_graph(self, error_rt =6,  losses=[-80]):
		graph= nx.DiGraph()
		for peak in self.peaks:
			graph.add_node(peak.plot(),  transition = peak.get_trans().toString(),  rt =peak.get_rt())
		for node, data in graph.nodes_iter(data=True):
			for node2,  data2 in graph.nodes_iter(data =True):
				if node2 != node:
					if asb(data['rt'] -data2['rt']) < error_rt:
						for loss in losses: 
							if float(data['transition'].split('/')[0]) + loss ==  float(data2['transition'].split('/')[0]):
								graph.add_edge(node,  node2,  weight =-80)
								
		
		#for node in graph:
		#	if not node.degree():
		#		resulting_graph.add_node(node)
		nx.draw(graph)
		nx.write_gml(graph,  "test.gml")
				
if __name__ =="__main__":		
	from parsers import lxml_target
	from model import integration_peaks
	from core import clustering as clust, processing as proc, objects as obj
	import sys
	#from model import integration_peaks
	p1=lxml_target.lxml_parser("output-s6.mzXML")
	p1.parsing(3.03)
	p2=lxml_target.lxml_parser("output-s12.mzXML")
	p2.parsing(3.03)
	lspl=obj.ListSample()
	lspl.append(p1.getSpl())
	lspl.append(p2.getSpl())
	integr = integration_peaks.integration_by_xcms(lspl)
	integr.transformation()
	integr.integrate_align(max=10, snthresh =3)
	proc.creating_peakList(lspl)
	d =Drawing_graph(lspl[0].get_peak_list(), 2)
	d.make_spl_graph()
	
